/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZGetInfoResult;
/*     */ import com.zimbra.client.ZInvite;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.soap.W3cDomUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.mail.message.BrowseRequest;
/*     */ import com.zimbra.soap.mail.message.BrowseResponse;
/*     */ import com.zimbra.soap.mail.message.CreateAppointmentRequest;
/*     */ import com.zimbra.soap.mail.message.CreateAppointmentResponse;
/*     */ import com.zimbra.soap.mail.message.GetMsgRequest;
/*     */ import com.zimbra.soap.mail.message.GetMsgResponse;
/*     */ import com.zimbra.soap.mail.message.SearchRequest;
/*     */ import com.zimbra.soap.mail.message.SearchResponse;
/*     */ import com.zimbra.soap.mail.message.SendInviteReplyRequest;
/*     */ import com.zimbra.soap.mail.message.SendInviteReplyResponse;
/*     */ import com.zimbra.soap.mail.type.AddRecurrenceInfo;
/*     */ import com.zimbra.soap.mail.type.AppointmentHitInfo;
/*     */ import com.zimbra.soap.mail.type.BrowseData;
/*     */ import com.zimbra.soap.mail.type.CalOrganizer;
/*     */ import com.zimbra.soap.mail.type.CalendarAttendee;
/*     */ import com.zimbra.soap.mail.type.CalendarAttendeeWithGroupInfo;
/*     */ import com.zimbra.soap.mail.type.DtTimeInfo;
/*     */ import com.zimbra.soap.mail.type.EmailAddrInfo;
/*     */ import com.zimbra.soap.mail.type.InstanceDataInfo;
/*     */ import com.zimbra.soap.mail.type.IntervalRule;
/*     */ import com.zimbra.soap.mail.type.InvitationInfo;
/*     */ import com.zimbra.soap.mail.type.InviteComponent;
/*     */ import com.zimbra.soap.mail.type.InviteComponentWithGroupInfo;
/*     */ import com.zimbra.soap.mail.type.InviteWithGroupInfo;
/*     */ import com.zimbra.soap.mail.type.MimePartInfo;
/*     */ import com.zimbra.soap.mail.type.Msg;
/*     */ import com.zimbra.soap.mail.type.MsgSpec;
/*     */ import com.zimbra.soap.mail.type.MsgWithGroupInfo;
/*     */ import com.zimbra.soap.mail.type.RecurrenceInfo;
/*     */ import com.zimbra.soap.mail.type.SimpleRepeatingRule;
/*     */ import com.zimbra.soap.type.SearchHit;
/*     */ import java.io.IOException;
/*     */ import java.text.ParseException;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.TimeZone;
/*     */ import javax.xml.bind.Marshaller;
/*     */ import junit.framework.TestCase;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpException;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ import org.apache.commons.httpclient.methods.StringRequestEntity;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.io.DocumentResult;
/*     */ import org.junit.Assert;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestJaxb
/*     */   extends TestCase
/*     */ {
/*     */   private static final String USER_NAME = "user1";
/*     */   private static final String ATTENDEE1 = "user1";
/*     */   private static final String ATTENDEE2 = "user2";
/*     */   private static final String ORGANIZER = "user3";
/*  89 */   private static final String NAME_PREFIX = TestJaxb.class.getSimpleName();
/*     */   private Marshaller marshaller;
/*     */   public static final String YMD_PATTERN = "yyyy-MM-dd";
/*     */   
/*     */   public void setUp() throws Exception
/*     */   {
/*  95 */     cleanUp();
/*  96 */     this.marshaller = JaxbUtil.createMarshaller();
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 101 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception {
/* 105 */     TestUtil.deleteTestData("user3", NAME_PREFIX);
/* 106 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/* 107 */     TestUtil.deleteTestData("user2", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   private static String getCN(ZMailbox mbox) throws ServiceException {
/* 111 */     String cn = (String)((List)mbox.getAccountInfo(false).getAttrs().get("cn")).get(0);
/* 112 */     if (cn == null) {
/* 113 */       cn = mbox.getName();
/*     */     }
/* 115 */     return cn;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void testAcceptSeriesDeclineInstance()
/*     */     throws Exception
/*     */   {
/* 133 */     String subject = NAME_PREFIX + " Daily";
/* 134 */     ZMailbox organizerBox = TestUtil.getZMailbox("user3");
/* 135 */     ZMailbox attendeeBox = TestUtil.getZMailbox("user1");
/* 136 */     ZMailbox attendee2Box = TestUtil.getZMailbox("user2");
/* 137 */     String organizerEmail = organizerBox.getName();
/*     */     
/*     */ 
/* 140 */     InviteComponent inviteComp = new InviteComponent();
/* 141 */     inviteComp.addAttendee(CalendarAttendee.createForAddressDisplaynameRolePartstatRsvp(attendeeBox.getName(), getCN(attendeeBox), "REQ", "NE", Boolean.valueOf(true)));
/*     */     
/*     */ 
/* 144 */     inviteComp.addAttendee(CalendarAttendee.createForAddressDisplaynameRolePartstatRsvp(attendee2Box.getName(), getCN(attendee2Box), "REQ", "NE", Boolean.valueOf(true)));
/*     */     
/*     */ 
/* 147 */     inviteComp.setStatus("CONF");
/* 148 */     inviteComp.setFreeBusy("B");
/* 149 */     inviteComp.setCalClass("PUB");
/* 150 */     inviteComp.setTransparency("O");
/* 151 */     inviteComp.setIsDraft(Boolean.valueOf(false));
/* 152 */     inviteComp.setIsAllDay(Boolean.valueOf(false));
/* 153 */     inviteComp.setDtStart(DtTimeInfo.createForDatetimeAndZone("20161008T130000", "Europe/London"));
/* 154 */     inviteComp.setDtEnd(DtTimeInfo.createForDatetimeAndZone("20161008T140000", "Europe/London"));
/* 155 */     inviteComp.setName(subject);
/* 156 */     inviteComp.setLocation("room 101");
/* 157 */     inviteComp.setOrganizer(CalOrganizer.createForAddress(organizerEmail));
/* 158 */     inviteComp.setRecurrence(RecurrenceInfo.create(AddRecurrenceInfo.create(SimpleRepeatingRule.createFromFrequencyAndInterval("DAI", IntervalRule.create(1)))));
/*     */     
/*     */ 
/* 161 */     InvitationInfo invite = new InvitationInfo();
/* 162 */     invite.setInviteComponent(inviteComp);
/* 163 */     EmailAddrInfo attendeeAddr = EmailAddrInfo.createForAddressPersonalAndAddressType(attendeeBox.getName(), getCN(attendeeBox), "t");
/*     */     
/* 165 */     EmailAddrInfo attendeeAddr2 = EmailAddrInfo.createForAddressPersonalAndAddressType(attendee2Box.getName(), getCN(attendee2Box), "t");
/*     */     
/* 167 */     MimePartInfo mimePart = MimePartInfo.createForContentType("multipart/alternative");
/* 168 */     mimePart.addMimePart(MimePartInfo.createForContentTypeAndContent("text/plain", "invite body"));
/* 169 */     mimePart.addMimePart(MimePartInfo.createForContentTypeAndContent("text/html", "<html><body><p><b>invite</b> body</p></body></html>"));
/*     */     
/* 171 */     Msg msg = new Msg();
/* 172 */     msg.setFolderId("10");
/* 173 */     msg.setInvite(invite);
/* 174 */     msg.addEmailAddress(attendeeAddr);
/* 175 */     msg.addEmailAddress(attendeeAddr2);
/* 176 */     msg.setSubject(subject);
/* 177 */     msg.setMimePart(mimePart);
/* 178 */     CreateAppointmentRequest createSeriesRequest = CreateAppointmentRequest.create(msg);
/* 179 */     CreateAppointmentResponse caResp = (CreateAppointmentResponse)organizerBox.invokeJaxb(createSeriesRequest);
/* 180 */     Assert.assertNotNull("JAXB CreateAppointmentResponse object", caResp);
/* 181 */     Assert.assertNotNull("JAXB CreateAppointmentResponse calItemId", caResp.getCalItemId());
/* 182 */     Assert.assertNotNull("JAXB CreateAppointmentResponse invId", caResp.getCalInvId());
/* 183 */     Assert.assertNotNull("JAXB CreateAppointmentResponse modified sequence ms", caResp.getModifiedSequence());
/* 184 */     Assert.assertNotNull("JAXB CreateAppointmentResponse rev", caResp.getRevision());
/* 185 */     ZMessage seriesInviteMsg = TestUtil.waitForMessage(attendeeBox, subject);
/* 186 */     Assert.assertNotNull("ZMessage for series invite", seriesInviteMsg);
/* 187 */     ZInvite seriesInvite = seriesInviteMsg.getInvite();
/* 188 */     Assert.assertNotNull("ZInvite for series invite", seriesInvite);
/*     */     
/*     */ 
/* 191 */     ZMessage seriesAcceptMsg = sendInviteReplyToSeries(attendeeBox, organizerBox, seriesInviteMsg.getId(), subject, "ACCEPT");
/*     */     
/* 193 */     Assert.assertNotNull("ZMessage for series accept", seriesAcceptMsg);
/*     */     
/*     */ 
/* 196 */     SendInviteReplyRequest sirReq = new SendInviteReplyRequest(seriesInviteMsg.getId(), 0, "DECLINE");
/*     */     
/* 198 */     sirReq.setIdentityId(attendeeBox.getAccountInfo(false).getId());
/* 199 */     sirReq.setExceptionId(DtTimeInfo.createForDatetimeAndZone("20161011T130000", "Europe/London"));
/* 200 */     sirReq.setUpdateOrganizer(Boolean.valueOf(true));
/* 201 */     attendeeAddr.setAddressType("f");
/* 202 */     mimePart = MimePartInfo.createForContentType("multipart/alternative");
/* 203 */     mimePart.addMimePart(MimePartInfo.createForContentTypeAndContent("text/plain", "I won't attend on Tuesday, October 11, 2016."));
/*     */     
/* 205 */     mimePart.addMimePart(MimePartInfo.createForContentTypeAndContent("text/html", "<html><body><p><b>I won't attend  on Tuesday, October 11, 2016</b></p></body></html>"));
/*     */     
/* 207 */     msg = new Msg();
/* 208 */     msg.setReplyType("r");
/* 209 */     msg.setIdentityId(attendeeBox.getAccountInfo(false).getId());
/* 210 */     EmailAddrInfo orgAddr = EmailAddrInfo.createForAddressPersonalAndAddressType(organizerBox.getName(), organizerBox.getName(), "t");
/*     */     
/* 212 */     msg.addEmailAddress(orgAddr);
/* 213 */     msg.addEmailAddress(attendeeAddr);
/* 214 */     String declineSubject = "Decline: " + subject;
/* 215 */     msg.setSubject(declineSubject);
/* 216 */     msg.setMimePart(mimePart);
/* 217 */     sirReq.setMsg(msg);
/* 218 */     SendInviteReplyResponse sirResp = (SendInviteReplyResponse)attendeeBox.invokeJaxb(sirReq);
/* 219 */     Assert.assertNotNull("JAXB SendInviteReplyResponse object", sirResp);
/* 220 */     ZMessage instanceDeclineMsg = TestUtil.waitForMessage(organizerBox, String.format("subject:\"%s\"", new Object[] { declineSubject }));
/*     */     
/* 222 */     Assert.assertNotNull("ZMessage for series accept", instanceDeclineMsg);
/* 223 */     seriesInviteMsg = TestUtil.waitForMessage(attendee2Box, subject);
/* 224 */     Assert.assertNotNull("ZMessage for series invite", seriesInviteMsg);
/* 225 */     seriesInvite = seriesInviteMsg.getInvite();
/* 226 */     Assert.assertNotNull("ZInvite for series invite", seriesInvite);
/*     */     
/*     */ 
/* 229 */     ZMessage seriesTentativeMsg = sendInviteReplyToSeries(attendee2Box, organizerBox, seriesInviteMsg.getId(), subject, "TENTATIVE");
/*     */     
/* 231 */     Assert.assertNotNull("ZMessage for series tentative", seriesTentativeMsg);
/*     */     
/*     */ 
/* 234 */     SearchRequest sReq = new SearchRequest();
/* 235 */     sReq.setSearchTypes("appointment");
/* 236 */     sReq.setCalItemExpandStart(Long.valueOf(ymdStringToDate("2016-10-09")));
/* 237 */     sReq.setCalItemExpandEnd(Long.valueOf(ymdStringToDate("2016-10-14")));
/* 238 */     sReq.setQuery(String.format("in:Calendar and subject:%s", new Object[] { subject }));
/* 239 */     SearchResponse sResp = (SearchResponse)organizerBox.invokeJaxb(sReq);
/* 240 */     List<SearchHit> hits = sResp.getSearchHits();
/* 241 */     Assert.assertNotNull("Organizer calendar Search hits at end", hits);
/* 242 */     Assert.assertEquals("Num Organizer calendar hits at end", 1L, hits.size());
/* 243 */     SearchHit orgCalHit = (SearchHit)hits.get(0);
/* 244 */     Assert.assertTrue(orgCalHit instanceof AppointmentHitInfo);
/* 245 */     AppointmentHitInfo orgApptHit = (AppointmentHitInfo)orgCalHit;
/* 246 */     String seriesInviteId = orgApptHit.getInvId();
/* 247 */     Assert.assertNotNull("Organizer Calendar at end - series invite id", seriesInviteId);
/* 248 */     List<InstanceDataInfo> instances = orgApptHit.getInstances();
/* 249 */     Assert.assertNotNull("Organizer Calendar at end - instances in expansion", instances);
/* 250 */     Assert.assertEquals("Organizer Calendar at end - number of instances in expansion", 5L, instances.size());
/*     */     
/* 252 */     String exceptionInviteId = ((InstanceDataInfo)instances.get(2)).getInvId();
/* 253 */     Assert.assertNotNull("Organizer Calendar at end - exception invite id", exceptionInviteId);
/* 254 */     String exceptionRidZ = ((InstanceDataInfo)instances.get(2)).getRecurIdZ();
/* 255 */     Assert.assertNotNull("Organizer Calendar at end - exception invite RecurIdZ", exceptionRidZ);
/*     */     
/*     */ 
/* 258 */     MsgSpec gmeMsgSpec = new MsgSpec(exceptionInviteId);
/* 259 */     gmeMsgSpec.setRecurIdZ(exceptionRidZ);
/* 260 */     GetMsgRequest gmeReq = new GetMsgRequest(gmeMsgSpec);
/* 261 */     GetMsgResponse gmeResp = (GetMsgResponse)organizerBox.invokeJaxb(gmeReq);
/* 262 */     List<InviteComponentWithGroupInfo> eInviteComps = gmeResp.getMsg().getInvite().getInviteComponents();
/* 263 */     Assert.assertEquals("Organizer Calendar at end - number of components in exception", 1L, eInviteComps.size());
/* 264 */     List<CalendarAttendeeWithGroupInfo> eAttendees = ((InviteComponentWithGroupInfo)eInviteComps.get(0)).getAttendees();
/* 265 */     Assert.assertEquals("Organizer Calendar at end - number of attendees in exception", 2L, eAttendees.size());
/* 266 */     for (CalendarAttendeeWithGroupInfo eAttendee : eAttendees) {
/* 267 */       String addr = eAttendee.getAddress();
/* 268 */       String ptst = eAttendee.getPartStat();
/* 269 */       if (addr.equals(attendeeBox.getName())) {
/* 270 */         Assert.assertEquals("exception attendee1 partstat", "DE", ptst);
/* 271 */       } else if (addr.equals(attendee2Box.getName())) {
/* 272 */         Assert.assertEquals("exception attendee2 partstat", "TE", ptst);
/*     */       } else {
/* 274 */         Assert.fail(String.format("Unexpected attendee in exception [%s]", new Object[] { addr }));
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 279 */     MsgSpec gmsMsgSpec = new MsgSpec(seriesInviteId);
/* 280 */     GetMsgRequest gmsReq = new GetMsgRequest(gmsMsgSpec);
/* 281 */     GetMsgResponse gmsResp = (GetMsgResponse)organizerBox.invokeJaxb(gmsReq);
/* 282 */     List<InviteComponentWithGroupInfo> sInviteComps = gmsResp.getMsg().getInvite().getInviteComponents();
/* 283 */     Assert.assertEquals("Organizer Calendar at end - number of components in series", 1L, sInviteComps.size());
/* 284 */     List<CalendarAttendeeWithGroupInfo> sAttendees = ((InviteComponentWithGroupInfo)sInviteComps.get(0)).getAttendees();
/* 285 */     Assert.assertEquals("Organizer Calendar at end - number of attendees in exception", 2L, sAttendees.size());
/* 286 */     for (CalendarAttendeeWithGroupInfo sAttendee : sAttendees) {
/* 287 */       String addr = sAttendee.getAddress();
/* 288 */       String ptst = sAttendee.getPartStat();
/* 289 */       if (addr.equals(attendeeBox.getName())) {
/* 290 */         Assert.assertEquals("exception attendee1 partstat", "AC", ptst);
/* 291 */       } else if (addr.equals(attendee2Box.getName())) {
/* 292 */         Assert.assertEquals("exception attendee2 partstat", "TE", ptst);
/*     */       } else {
/* 294 */         Assert.fail(String.format("Unexpected attendee in exception [%s]", new Object[] { addr }));
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/* 300 */   private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
/*     */   
/*     */   private static long ymdStringToDate(String ymdString) {
/* 303 */     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
/* 304 */     formatter.setTimeZone(GMT);
/*     */     try
/*     */     {
/* 307 */       Date date = formatter.parse(ymdString);
/* 308 */       return date.getTime();
/*     */     } catch (ParseException e) {
/* 310 */       Assert.fail(String.format("Failed to convert string %s to long - exception %s", new Object[] { ymdString, e.getMessage() }));
/*     */     }
/* 312 */     return 0L;
/*     */   }
/*     */   
/*     */   private ZMessage sendInviteReplyToSeries(ZMailbox attendeeBox, ZMailbox organizerBox, String inviteMsgId, String subjectSuffix, String verb)
/*     */     throws Exception
/*     */   {
/* 318 */     SendInviteReplyRequest sirReq = new SendInviteReplyRequest(inviteMsgId, 0, verb);
/* 319 */     sirReq.setIdentityId(attendeeBox.getAccountInfo(false).getId());
/* 320 */     sirReq.setUpdateOrganizer(Boolean.valueOf(true));
/* 321 */     EmailAddrInfo attendeeAddr = EmailAddrInfo.createForAddressPersonalAndAddressType(attendeeBox.getName(), getCN(attendeeBox), "f");
/*     */     
/* 323 */     EmailAddrInfo orgAddr = EmailAddrInfo.createForAddressPersonalAndAddressType(organizerBox.getName(), organizerBox.getName(), "t");
/*     */     
/* 325 */     MimePartInfo mimePart = MimePartInfo.createForContentType("multipart/alternative");
/*     */     String subject;
/*     */     String plainText;
/* 328 */     String subject; if ("ACCEPT".equals(verb)) {
/* 329 */       String plainText = "Yes, I will attend.";
/* 330 */       subject = "Accept: " + subjectSuffix; } else { String subject;
/* 331 */       if ("DECLINE".equals(verb)) {
/* 332 */         String plainText = "No, I won't attend.";
/* 333 */         subject = "Decline: " + subjectSuffix;
/*     */       } else {
/* 335 */         plainText = "Maybe, I will attend.";
/* 336 */         subject = "Tentative: " + subjectSuffix;
/*     */       } }
/* 338 */     String htmlText = String.format("<html><body><p><b>%s</b></p></body></html>", new Object[] { plainText });
/* 339 */     mimePart.addMimePart(MimePartInfo.createForContentTypeAndContent("text/plain", plainText));
/* 340 */     mimePart.addMimePart(MimePartInfo.createForContentTypeAndContent("text/html", htmlText));
/* 341 */     Msg msg = new Msg();
/* 342 */     msg.setReplyType("r");
/* 343 */     msg.setIdentityId(attendeeBox.getAccountInfo(false).getId());
/* 344 */     msg.addEmailAddress(orgAddr);
/* 345 */     msg.addEmailAddress(attendeeAddr);
/* 346 */     msg.setSubject(subject);
/* 347 */     msg.setMimePart(mimePart);
/* 348 */     sirReq.setMsg(msg);
/* 349 */     SendInviteReplyResponse sirResp = (SendInviteReplyResponse)attendeeBox.invokeJaxb(sirReq);
/* 350 */     Assert.assertNotNull("JAXB SendInviteReplyResponse object", sirResp);
/* 351 */     return TestUtil.waitForMessage(organizerBox, String.format("subject:\"%s\"", new Object[] { subject }));
/*     */   }
/*     */   
/*     */   private String envelope(String authToken, String requestBody) {
/* 355 */     return "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><soap:Header><context xmlns=\"urn:zimbra\"><userAgent name=\"Zimbra Junit\" version=\"0.0\"/><authToken>" + authToken + "</authToken>" + "<authTokenControl voidOnExpired=\"1\"/>" + "<nosession/>" + "</context>" + "</soap:Header>" + "<soap:Body>" + requestBody + "</soap:Body>" + "</soap:Envelope>";
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private Object sendReq(String requestBody, String requestCommand)
/*     */     throws HttpException, IOException, ServiceException
/*     */   {
/* 372 */     HttpClient client = new HttpClient();
/* 373 */     PostMethod post = new PostMethod(TestUtil.getSoapUrl() + requestCommand);
/* 374 */     post.setRequestEntity(new StringRequestEntity(requestBody, "application/soap+xml", "UTF-8"));
/* 375 */     int respCode = HttpClientUtil.executeMethod(client, post);
/* 376 */     Assert.assertEquals(200L, respCode);
/* 377 */     com.zimbra.common.soap.Element envelope = W3cDomUtil.parseXML(post.getResponseBodyAsStream());
/* 378 */     SoapProtocol proto = SoapProtocol.determineProtocol(envelope);
/* 379 */     com.zimbra.common.soap.Element doc = proto.getBodyElement(envelope);
/* 380 */     return JaxbUtil.elementToJaxb(doc);
/*     */   }
/*     */   
/*     */   private com.zimbra.common.soap.Element sendReqExpectingFail(String requestBody, String requestCommand, int expectedRespCode) throws HttpException, IOException, ServiceException
/*     */   {
/* 385 */     HttpClient client = new HttpClient();
/* 386 */     PostMethod post = new PostMethod(TestUtil.getSoapUrl() + requestCommand);
/* 387 */     post.setRequestEntity(new StringRequestEntity(requestBody, "application/soap+xml", "UTF-8"));
/* 388 */     int respCode = HttpClientUtil.executeMethod(client, post);
/* 389 */     Assert.assertEquals(expectedRespCode, respCode);
/* 390 */     return W3cDomUtil.parseXML(post.getResponseBodyAsStream());
/*     */   }
/*     */   
/*     */   public BrowseResponse doBrowseRequest(BrowseRequest browseRequest) throws Exception {
/* 394 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 395 */     String authToken = mbox.getAuthToken().getValue();
/* 396 */     DocumentResult dr = new DocumentResult();
/* 397 */     this.marshaller.marshal(browseRequest, dr);
/* 398 */     Document doc = dr.getDocument();
/* 399 */     ZimbraLog.test.info(doc.getRootElement().asXML());
/* 400 */     return (BrowseResponse)sendReq(envelope(authToken, doc.getRootElement().asXML()), "BrowseRequest");
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testBrowseRequestDomains() throws Exception {
/* 405 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 406 */     TestUtil.addMessage(mbox, NAME_PREFIX);
/* 407 */     BrowseRequest browseRequest = new BrowseRequest("domains", "", Integer.valueOf(10));
/* 408 */     BrowseResponse browseResponse = doBrowseRequest(browseRequest);
/* 409 */     Assert.assertNotNull("JAXB BrowseResponse object", browseResponse);
/* 410 */     List<BrowseData> datas = browseResponse.getBrowseDatas();
/* 411 */     Assert.assertNotNull("JAXB BrowseResponse datas", datas);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testBrowseRequestAttachments() throws Exception {
/* 416 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 417 */     TestUtil.addMessage(mbox, NAME_PREFIX);
/* 418 */     BrowseRequest browseRequest = new BrowseRequest("attachments", "", Integer.valueOf(10));
/* 419 */     BrowseResponse browseResponse = doBrowseRequest(browseRequest);
/* 420 */     Assert.assertNotNull("JAXB BrowseResponse object", browseResponse);
/* 421 */     List<BrowseData> datas = browseResponse.getBrowseDatas();
/* 422 */     Assert.assertNotNull("JAXB BrowseResponse datas", datas);
/*     */   }
/*     */   
/* 425 */   static String bodyWithObject = "contains http://www.example.com/fun so treat as containing com_zimbra_url object";
/*     */   
/*     */   @Test
/* 428 */   public void testBrowseRequestObjects() throws Exception { ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 429 */     MessageBuilder mb = new MessageBuilder();
/* 430 */     String raw = mb.withSubject(NAME_PREFIX).withBody(bodyWithObject).withContentType("text/plain").create();
/*     */     
/* 432 */     TestUtil.addRawMessage(mbox, raw);
/* 433 */     BrowseRequest browseRequest = new BrowseRequest("objects", "", Integer.valueOf(10));
/* 434 */     BrowseResponse browseResponse = doBrowseRequest(browseRequest);
/* 435 */     Assert.assertNotNull("JAXB BrowseResponse object", browseResponse);
/* 436 */     List<BrowseData> datas = browseResponse.getBrowseDatas();
/* 437 */     Assert.assertNotNull("JAXB BrowseResponse datas", datas);
/* 438 */     Assert.assertTrue("BrowseDatas size should be greater than 1", datas.size() >= 1);
/*     */   }
/*     */   
/*     */   public com.zimbra.common.soap.Element doBadBrowseRequest(BrowseRequest browseRequest) throws Exception {
/* 442 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 443 */     String authToken = mbox.getAuthToken().getValue();
/* 444 */     DocumentResult dr = new DocumentResult();
/* 445 */     this.marshaller.marshal(browseRequest, dr);
/* 446 */     Document doc = dr.getDocument();
/* 447 */     ZimbraLog.test.info(doc.getRootElement().asXML());
/* 448 */     return sendReqExpectingFail(envelope(authToken, doc.getRootElement().asXML()), "BrowseRequest", 500);
/*     */   }
/*     */   
/* 451 */   static String BAD_REGEX = ".*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*822";
/*     */   
/*     */   @Test
/*     */   public void testBrowseRequestDomainsBadRegex() throws Exception {
/* 455 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 456 */     TestUtil.addMessage(mbox, NAME_PREFIX);
/* 457 */     BrowseRequest browseRequest = new BrowseRequest("domains", BAD_REGEX, Integer.valueOf(10));
/* 458 */     com.zimbra.common.soap.Element envelope = doBadBrowseRequest(browseRequest);
/* 459 */     Assert.assertNotNull("Envelope", envelope);
/* 460 */     Assert.assertTrue("Error contained in SOAP response", envelope.toString().contains("regular expression match involved more than 100000 accesses for pattern"));
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testBrowseRequestObjectsBadRegex()
/*     */     throws Exception
/*     */   {
/* 467 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 468 */     MessageBuilder mb = new MessageBuilder();
/* 469 */     String raw = mb.withSubject(NAME_PREFIX).withBody(bodyWithObject).withContentType("text/plain").create();
/*     */     
/* 471 */     TestUtil.addRawMessage(mbox, raw);
/* 472 */     BrowseRequest browseRequest = new BrowseRequest("objects", "", Integer.valueOf(10));
/* 473 */     BrowseResponse browseResponse = doBrowseRequest(browseRequest);
/* 474 */     Assert.assertTrue("BrowseDatas size should be greater than 1", browseResponse.getBrowseDatas().size() >= 1);
/* 475 */     browseRequest = new BrowseRequest("objects", BAD_REGEX, Integer.valueOf(10));
/* 476 */     com.zimbra.common.soap.Element envelope = doBadBrowseRequest(browseRequest);
/* 477 */     Assert.assertNotNull("Envelope", envelope);
/* 478 */     Assert.assertTrue("Error contained in SOAP response", envelope.toString().contains("regular expression match involved more than 100000 accesses for pattern"));
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testBrowseRequestAttachmentsBadRegex()
/*     */     throws Exception
/*     */   {
/* 485 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 486 */     TestUtil.addMessage(mbox, NAME_PREFIX);
/* 487 */     BrowseRequest browseRequest = new BrowseRequest("attachments", BAD_REGEX, Integer.valueOf(10));
/* 488 */     com.zimbra.common.soap.Element envelope = doBadBrowseRequest(browseRequest);
/* 489 */     Assert.assertNotNull("Envelope", envelope);
/* 490 */     Assert.assertTrue("Error contained in SOAP response", envelope.toString().contains("regular expression match involved more than 100000 accesses for pattern"));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestJaxb.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */